/*
 * Copyright (c) 2009 Team TypeZero. All rights reserved.
 * 
 * This file is part of [ProjectName].
 * 
 * [ProjectName] is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * [ProjectName] is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with [ProjectName].  If not, see <http://www.gnu.org/licenses/>.
 */

package hangman;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

import java.util.TreeSet;
import javax.swing.JPanel;

/**
 *
 * @author markvismonte
 */
public class HangmanBoard extends JPanel{
    private ArrayList<String> words;
    private String currentWord;
    private TreeSet<Character> rightLetters;
    private String wrongLetters;
    private int state;
    private boolean lose, win;


    public HangmanBoard()
        {
        super();
        words = new ArrayList<String>();
        setDoubleBuffered(true);
        addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (lose || win)
                    {
                    wrongLetters = "";
                    currentWord = words.get((int)(Math.random() * words.size()));
                    System.out.println(currentWord);
                    rightLetters = new TreeSet<Character>();
                    state = 0;
                    lose = win = false;
                    repaint();
                    }
            }

            });
        addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                if (!Character.isLetter(e.getKeyChar()))
                    return;
                char x = Character.toLowerCase(e.getKeyChar());
                String lastChar = x + "";
                if (currentWord.indexOf(lastChar) != -1)
                    {
                    rightLetters.add(x);
                    repaint();
                    }
                else
                    {
                    if (wrongLetters.indexOf(lastChar) == -1)
                        {
                        wrongLetters += lastChar;
                        if (!lose)
                            state++;
                        repaint();
                        }
                    }
            }
            });
        setBackground(new Color(74,165, 255));
        setFocusable(true);
        try{
        Scanner input = new Scanner(new File("words.txt"));
        while (input.hasNext())
            words.add(input.next().toLowerCase());
        }
        catch (FileNotFoundException ex) {}
        wrongLetters = "";
        currentWord = words.get((int)(Math.random() * words.size()));
        System.out.println(currentWord);
        rightLetters = new TreeSet<Character>();
        state = 0;
        lose = win = false;
        }

    private boolean containsChar(char a, TreeSet<Character> b)
        {
        for (char x: b)
            if (a == x)
                return true;
        return false;
        }

    @Override
    public void paint(Graphics g)
        {
        super.paint(g);
        g.setColor(Color.BLACK);
        g.drawString("Word:", 200, 50);
        int lettercount = 0;
        for (int i = 0, x = 200, y = 80; i < currentWord.length(); i++, x+= 15)
            {
            g.drawString("_", x, y);
            if (containsChar(currentWord.charAt(i), rightLetters) || lose)
                {
                g.drawString(currentWord.charAt(i) + "", x, y - 10);
                lettercount++;
                }
            }

        g.drawString("Wrong Letters", 50, 50);
        g.drawString(wrongLetters, 50, 70);
        win = currentWord.length() == lettercount;
        if (lose)
            g.drawString("YOU LOSE! Click to play again", 50, 20);
        else
            {
            if (win)
                g.drawString("YOU WIN! Click to play again", 50, 20);
            }
        switch (state)
            {
            //right leg
            case 6:
                g.drawLine(50, 250, 70, 270);
            //left leg
            case 5:
                g.drawLine(50, 250, 30, 270);
            //right arm
            case 4:
                g.drawLine(50, 230, 70, 230);
            //left arm
            case 3:
                g.drawLine(50, 230, 30, 230);
            //body
            case 2:
                g.drawLine(50, 210, 50, 250);
            //head
            case 1:
                g.drawOval(30, 170, 40, 40);
            //gallow
            default:
                g.fillRect(10,150,10,150);
                g.drawLine(10, 150, 50, 150);
                g.drawLine(50, 150, 50, 170);
            }
        if (state >= 6)
            lose = true;
        }


}
